package com.du.lin.service.impl;

import com.du.lin.bean.SalesManagement;
import com.du.lin.bean.User;
import com.du.lin.dao.SalesManagementMapper;
import com.du.lin.dao.UserMapper;
import com.du.lin.service.ElectricitySalesManagementService;
import com.du.lin.utils.ConventMessageUtil;
import com.du.lin.utils.JqgridUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

@Service
public class ElectricitySalesManagementServiceImpl implements ElectricitySalesManagementService {

    @Autowired
    private SalesManagementMapper mapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JqgridUtil jqgridUtil;

    @Override
    public List<SalesManagement> getAllSalesManagement(String dept,String starttime) {
        return mapper.getAllSalesManagements( dept, starttime);
    }

    @Override
    public String getAllUserJson(int page , int count,String dept,String starttime) {
        List<SalesManagement> all = getAllSalesManagement (dept,starttime);
        List<User> userList = userMapper.findUserByDeptId(dept);
        List<Object> allList = new ArrayList<>();
        try {
             allList = ConventMessageUtil.RepeatMessage(all,userList,starttime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int toIndex = count * page;
        if (allList.size() < toIndex) {
            toIndex = allList.size();
        }
        List<Object> list = allList.subList(count * (page - 1), toIndex);;
        return jqgridUtil.getJson(list, page + "", allList.size() , count);
    }

    /**
     * 添加信息
     * @param value
     * @param userId
     * @param date
     * @param isType
     * @return
     */
    @Override
    public String addSalesManagerment(String value, String userId, String date, String isType) {
        final String[] resutlStr = new String[]{"0"};
        //要处理的
        /*List<Map<String,String>> typeList = new ArrayList<>();
        Map<String,String> dxs = new HashMap<>();
        dxs.put("typeName","电销数");
        dxs.put("isType","dxs");
        typeList.add(dxs);
        Map<String,String> jwxrs = new HashMap<>();
        jwxrs.put("typeName","加微信人数");
        jwxrs.put("isType","jwxrs");
        typeList.add(jwxrs);
        Map<String,String> lkhgs = new HashMap<>();
        lkhgs.put("typeName","老客户个数");
        lkhgs.put("isType","lkhgs");
        typeList.add(lkhgs);
        Map<String,String> xkhgs = new HashMap<>();
        xkhgs.put("typeName","新客户个数");
        xkhgs.put("isType","xkhgs");
        typeList.add(xkhgs);
        Map<String,String> lkhsqgs = new HashMap<>();
        lkhsqgs.put("typeName","老客户申请数");
        lkhsqgs.put("isType","lkhsqgs");
        typeList.add(lkhsqgs);
        Map<String,String> xkhsqgs = new HashMap<>();
        xkhsqgs.put("typeName","新客户申请数");
        xkhsqgs.put("isType","xkhsqgs");
        typeList.add(lkhsqgs);
        Map<String,String> lkhxkgs = new HashMap<>();
        lkhxkgs.put("typeName","老客户下款数");
        lkhxkgs.put("isType","lkhxkgs");
        typeList.add(lkhxkgs);
        Map<String,String> xkhxkgs = new HashMap<>();
        xkhxkgs.put("typeName","新客户下款数");
        xkhxkgs.put("isType","xkhxkgs");
        typeList.add(xkhxkgs);
*/

        switch (isType){
            case "dxs":
                   SalesManagement salesManagement1 =  mapper.getSalesManagements(userId,date);
                   if(StringUtils.isEmpty(salesManagement1)){
                       resutlStr[0] = mapper.insertSalesManagementsByNumber(userId, date, value)+"";
                   }else {
                       resutlStr[0] = mapper.updateSalesManagementsByNumber(String.valueOf(salesManagement1.getId()),value)+"";
                   }
                break;
            case "jwxrs":
                    SalesManagement salesManagement2 =  mapper.getSalesManagements(userId,date);
                    if(StringUtils.isEmpty(salesManagement2)){
                        resutlStr[0] =  mapper.insertSalesManagementsByWxNumber(userId, date, value)+"";
                    }else {
                        resutlStr[0] =  mapper.updateSalesManagementsByWxNumberr(String.valueOf(salesManagement2.getId()),value)+"";
                    }
                break;
            case "lkhgs":
                    SalesManagement salesManagement3 =  mapper.getSalesManagements(userId,date);
                    if(StringUtils.isEmpty(salesManagement3)){
                        resutlStr[0] =  mapper.insertSalesManagementsByOldCustomersNumber(userId, date, value)+"";
                    }else {
                        resutlStr[0] =  mapper.updateSalesManagementsByoldCustomersNumber(String.valueOf(salesManagement3.getId()),value)+"";
                    }
                break;
            case "xkhgs":
                    SalesManagement salesManagement4 =  mapper.getSalesManagements(userId,date);
                    if(StringUtils.isEmpty(salesManagement4)){
                        resutlStr[0] =  mapper.insertSalesManagementsByNewCustomersNumber(userId, date, value)+"";
                    }else {
                        resutlStr[0] =  mapper.updateSalesManagementsBynewCustomersNumber(String.valueOf(salesManagement4.getId()),value)+"";
                    }
                break;
            case "lkhsqgs":
                    SalesManagement salesManagement5 =  mapper.getSalesManagements(userId,date);
                    if(StringUtils.isEmpty(salesManagement5)){
                        resutlStr[0] = mapper.insertSalesManagementsByOldApplyNumber(userId, date, value)+"";
                    }else {
                        resutlStr[0] =  mapper.updateSalesManagementsByoldApplyNumber(String.valueOf(salesManagement5.getId()),value)+"";
                    }
                break;
            case "xkhsqgs":
                    SalesManagement salesManagement6 =  mapper.getSalesManagements(userId,date);
                    if(StringUtils.isEmpty(salesManagement6)){
                        resutlStr[0] = mapper.insertSalesManagementsByNewApplyNumber(userId, date, value)+"";
                    }else {
                        resutlStr[0] = mapper.updateSalesManagementsBynewApplyNumber(String.valueOf(salesManagement6.getId()),value)+"";
                    }
                break;
            case "lkhxkgs":
                    SalesManagement salesManagement7 =  mapper.getSalesManagements(userId,date);
                    if(StringUtils.isEmpty(salesManagement7)){
                        resutlStr[0] =   mapper.insertSalesManagementsByOldParagraphNumber(userId, date, value)+"";
                    }else {
                        resutlStr[0] =  mapper.updateSalesManagementsByoldParagraphNumberr(String.valueOf(salesManagement7.getId()),value)+"";
                    }
                break;
            case "xkhxkgs":
                    SalesManagement salesManagement8 =  mapper.getSalesManagements(userId,date);
                    if(StringUtils.isEmpty(salesManagement8)){
                        resutlStr[0] =  mapper.insertSalesManagementsByNewParagraphNumber(userId, date, value)+"";
                    }else {
                        resutlStr[0] =  mapper.updateSalesManagementsBynewParagraphNumber(String.valueOf(salesManagement8.getId()),value)+"";
                    }
                break;
        }


        return resutlStr[0];
    }

    @Override
    public List<Object> getList(String dept, String starttime) {
        List<SalesManagement> all = getAllSalesManagement (dept,starttime);
        List<User> userList = userMapper.findUserByDeptId(dept);
        List<Object> allList = new ArrayList<>();
        try {
            allList = ConventMessageUtil.RepeatMessage(all,userList,starttime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return allList;
    }

    @Override
    public int deletesales(String userId, String date) {
        return mapper.deletesales(userId,date);
    }

}
